package joinpay

import (
	"encoding/json"
	"hotgo/utility/joinpay/utils"
)

const SinglePayQueryApi = "https://www.joinpay.com/payment/pay/accountBalanceQuery" // 可取余额查询

type SinglePayQueryReq struct {
	UserNo          string `json:"userNo"`
	MerchantOrderNo string `json:"merchantOrderNo"`
	Hmac            string `json:"hmac"`
}

type SinglePayQueryResp struct {
	StatusCode int    `json:"statusCode"`
	Message    string `json:"message"`
	Data       struct {
		Status               int     `json:"status"`
		ErrorCode            string  `json:"errorCode"`
		ErrorDesc            string  `json:"errorDesc"`
		UserNo               string  `json:"userNo"`
		MerchantOrderNo      string  `json:"merchantOrderNo"`
		PlatformSerialNo     string  `json:"platformSerialNo"`
		ReceiverAccountNoEnc string  `json:"receiverAccountNoEnc"`
		ReceiverNameEnc      string  `json:"receiverNameEnc"`
		PaidAmount           float64 `json:"paidAmount"`
		Fee                  float64 `json:"fee"`
		Hmac                 string  `json:"hmac"`
	} `json:"data"`
}

// SinglePayQuery 单笔代付查询接口
func SinglePayQuery(merchantKey string, req SinglePayQueryReq) (*SinglePayQueryResp, error) {
	marshal, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}
	body, err := utils.PostRequest(SinglePayQueryApi, marshal)
	if err != nil {
		return nil, err
	}
	var resp *SinglePayQueryResp
	err = json.Unmarshal(body, &resp)

	return resp, err
}
